package com.ymatou.iapi.accounting.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ymatou.iapi.accounting.parameter.AccountEntryBean;
import com.ymatou.iapi.accounting.parameter.response.GetAccountEntryListResponse;
import com.ymatou.utils.EnvSetup;
import com.ymt.base.YmatouBaseCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 获取账户资金流水接口Call FreeMaker Template自动生成代码
 */
public class GetAccountEntryListCall extends YmatouBaseCall {
	private static String opurl = "api/Account/GetAccountEntryList";

	public GetAccountEntryListCall() {
		super(EnvSetup.getSystemUrl("accounting_iapi"), opurl, "POST", "JSON");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept", "text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	// get statuscode
	public int getStatuscode() {
		int status = 0;
		try {
			status = new JSONObject(getReturnData()).getInt("StatusCode");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return status;

	}

	// get msg
	public String getMsg() {
		String msg = null;
		try {
			msg = new JSONObject(getReturnData()).getString("Message");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return msg;

	}

	// get data
	public JSONObject getData() {
		JSONObject data = null;
		try {
			data = new JSONObject(getReturnData()).getJSONObject("Data");
		} catch (JSONException e) {
			Logger.debug("Ts_GetAccountEntryList return data is null!");
		}
		return data;
	}

	public GetAccountEntryListResponse getAccountEntryListData() {
		GetAccountEntryListResponse data = null;
		try {
			data = (GetAccountEntryListResponse) FormatUtil.GSONToObject(getString("Data"),
					GetAccountEntryListResponse.class);
		} catch (Exception e) {
			Logger.debug("GetAccountEntryListResponse not found!!");
			e.printStackTrace();
		}
		return data;
	}

	// get totalRecords
	public int getTotalRecords() {
		int totalRecords = 0;
		try {
			totalRecords = getData().getInt("TotalRecords");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return totalRecords;
	}

	// get TotalPages
	public int getTotalPages() {
		int totalPages = 0;
		try {
			totalPages = getData().getInt("TotalPages");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return totalPages;
	}

	// get accountList
	public String getAccountEntryList() {
		String accountEntryList = null;
		try {
			accountEntryList = getData().getString("AccountEntryList");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return accountEntryList;
	}

	public ArrayList<AccountEntryBean> getReturnAccountList() throws JSONException {
		ArrayList<AccountEntryBean> list = null;
		String accountList = getAccountEntryList();
		if (accountList != null) {
			Gson gson = new Gson();
			list = gson.fromJson(accountList, new TypeToken<ArrayList<AccountEntryBean>>() {
			}.getType());
		}
		return list;
	}

}